package com.java.basis.thread.n3;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wangning
 * @create 2022-01-24 10:56
 */
@Slf4j(topic = "c.TestReentrantLock02")
public class TestReentrantLock03 {
	private static ReentrantLock lock = new ReentrantLock();

	public static void main(String[] args) throws InterruptedException {
		Thread t1 = new Thread(() -> {
			log.debug("尝试获得锁");

//			boolean b = lock.tryLock();//可以带参数，设置获得锁尝试的时间
			boolean b = false;
			try {
				b = lock.tryLock(1, TimeUnit.SECONDS);
			} catch (InterruptedException e) {
				e.printStackTrace();
				log.debug("获取不到锁");
				return;
			}

			if(!b) {
				log.debug("获取不到锁");
				return;
			}
			try {
				log.debug("获得到锁");
			} finally {

				lock.unlock();
			}

		}, "t1");
		lock.lock();

		t1.start();
		lock.unlock();

	}



}
